package com.webblog.util;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.UUID;

public class JWTUtil {

    private static long time = 1000*60*60*24;
//    private static long time = 1000*10;
    private static String signature = "admin";


    public static String  createToken(int userId,String userName){
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtTolen = jwtBuilder
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                .claim("username",userName)
                .claim("userId",userId)
                .setSubject("admin-test")
                .setExpiration(new Date(System.currentTimeMillis()+time))
                .setId(UUID.randomUUID().toString())
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();
        return jwtTolen;
    }


    public static boolean checkToken(String token){
        if(token == null){
            return false;
        }
        else {
            try {
                Jws<Claims> claimsJws = Jwts.parser().setSigningKey(signature).parseClaimsJws(token);
            } catch (Exception e) {
                return false;
            }
            return true;
        }

    }
}
